package boot.spring.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import boot.spring.pagemodel.MSG;
import boot.spring.po.City;
import boot.spring.repository.CityRepository;
import boot.spring.service.CityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "索引的接口")
@Controller
public class IndexController {
	
	
	@Autowired
	CityService cityService;
	
	
	@Autowired
	CityRepository cityRepository;
	
	@ApiOperation("分页导入城市表索引文档")
	@RequestMapping(value="/city",method = RequestMethod.GET)
	@ResponseBody
	public MSG indexCity() {
		List<City> list = cityService.listAll();
		float total = list.size();
		float pagesize = 10;
		int pagenum = (int)Math.ceil(total/pagesize);
		System.out.println("一共有页数" + pagenum);
		for (int i = 1; i<= pagenum; i++) {
			List<City> page = cityService.listAll(i, 10);
			System.out.println("正在导入第" + i + "页数据");
			if (page.size()>0) {
				page.stream().forEach(city -> {
					String d = city.getLastUpdate();
					d = d.substring(0, d.length() -2);
					city.setLastUpdate(d);
					System.out.println(city);
				});
				cityRepository.saveAll(page);
			}
		}
		return new MSG("index success");
	}
}
